﻿using System.Drawing;
using System.Windows.Forms;

namespace StockLib.Indicator
{
    public partial class StockVolIndicator : StockIndicator
    {
        public override void DrawGraphTitle(Graphics g, Rectangle rect)
        {
            int y = (rect.Top + rect.Bottom) / 2;
            int x = rect.Left;
            string textOutput = string.Format("{0}{1:#0}", "VOLUME:", kData[indexEnd].Volume);
            TextRenderer.DrawText(g, textOutput, font, new Point(x, y), StockProfile.Colors.GraphTextColor, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
            x += TextRenderer.MeasureText(textOutput, font).Width;


            float[] ma = CalcOutput(indexEnd);
            for (int i = 0; i < maDays.Count; i++)
            {
                Color color = StockProfile.IndicatorColors[i];
                textOutput = string.Format("MA{0}:{1:#0}", maDays[i], ma[i]);
                TextRenderer.DrawText(g, textOutput, font, new Point(x, y), color, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                x += TextRenderer.MeasureText(textOutput, font).Width;
            }
        }

        public override void DrawGraphData(Graphics g)
        {
            for (int index = indexStart; index <= indexEnd; index++)
            {
                KData data = kData[index];

                // 绘制实体线
                Rectangle kRect = new Rectangle();
                kRect.X = GetXPos(index) - thickness;
                kRect.Y = GetYPos(data.Volume);
                kRect.Width = thickness * 2;
                kRect.Height = rectAll.Bottom - kRect.Y - graphMargin.Bottom;

                if (thickness <= 0)
                    kRect.Width = 1;

                if (data.Close < data.Open)
                {
                    g.FillRectangle(StockProfile.Brushes.DnLineBrush, kRect);
                    g.DrawRectangle(StockProfile.Pens.DnLinePen, kRect);
                }
                else
                {
                    g.FillRectangle(StockProfile.Brushes.BackBrush, kRect);
                    g.DrawRectangle(StockProfile.Pens.UpLinePen, kRect);
                }

                // 绘制MA
                if (index > 1)
                {
                    float[] ma = CalcOutput(index);
                    float[] maLast = CalcOutput(index - 1);

                    for (int i = 0; i < maDays.Count; i++)
                    {
                        if (index > maDays[i])
                        {
                            int xPos = GetXPos(index);
                            int xPosLast = GetXPos(index - 1);
                            int yPos = GetYPos(ma[i]);
                            int yPosLast = GetYPos(maLast[i]);
                            g.DrawLine(StockProfile.IndicatorPens[i], xPosLast, yPosLast, xPos, yPos);
                        }
                    }
                }
            }
        }

        protected override void PrepareDrawSetting(Rectangle rectAll)
        {
            base.PrepareDrawSetting(rectAll);

            graphMargin.Top = graphMargin.Bottom = 5;
        }
    }
}
